package com.link2room.jetfire.facade.impl;

import com.link2room.blaster.constant.MsgConstant;
import com.link2room.blaster.util.MessageFlatPad;
import com.link2room.bumblebee.dto.param.base.PmsReservationOptionParamDto;
import com.link2room.bumblebee.dto.result.base.PmsReservationOptionDto;
import com.link2room.common.constant.BaseConstant;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.mns.MNSUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.enums.exception.JetfireSysExceptionEnum;
import com.link2room.jetfire.constant.JetfireMnsConstant;
import com.link2room.jetfire.constant.ReservationConstant;
import com.link2room.jetfire.constant.ReservationPromotionConstant;
import com.link2room.jetfire.dto.param.base.ReservationParamDto;
import com.link2room.jetfire.dto.param.inside.ReservationBaseInsideParamDto;
import com.link2room.jetfire.dto.param.inside.ReservationNmsStaInsideParamDto;
import com.link2room.jetfire.dto.param.inside.ReservationPayInsideParamDto;
import com.link2room.jetfire.dto.param.manager.ReservationManagerParamDto;
import com.link2room.jetfire.dto.result.base.*;
import com.link2room.jetfire.dto.result.extra.L2RPromotionDto;
import com.link2room.jetfire.entity.*;
import com.link2room.jetfire.enums.ReservationConfirmStaEnum;
import com.link2room.jetfire.enums.ReservationPbChannelEnum;
import com.link2room.jetfire.enums.ReservationResStaEnum;
import com.link2room.jetfire.enums.ReservationResTypeEnum;
import com.link2room.jetfire.exception.JetfireException;
import com.link2room.jetfire.facade.IReservationFacadeService;
import com.link2room.jetfire.feign.bumblebee.PmsReservationOptionFeignService;
import com.link2room.jetfire.feign.trailbreaker.RefundManageFeignService;
import com.link2room.jetfire.service.IReservationService;
import com.link2room.optimus.constant.BaseRateplanConstant;
import com.link2room.optimus.constant.OptimusMnsConstant;
import com.link2room.optimus.dto.result.base.L2RCancelRuleBaseDto;
import com.link2room.optimus.dto.result.base.L2RCancelRuleDto;
import com.link2room.rodimus.constant.RodimusMnsConstant;
import com.link2room.rodimus.dto.param.base.PointBaseParamDto;
import com.link2room.soundwave.constant.SoundwaveMnsConstant;
import com.link2room.trailbreaker.constant.TrailbreakerMnsConstant;
import com.link2room.trailbreaker.dto.paramDto.RefundManageApplyRefundParamDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Created by sss on 2016/9/20.
 */
@Component("reservationFacadeService")
public class ReservationFacadeServiceImpl implements IReservationFacadeService {

    @Resource
    private IReservationService reservationService;

    @Resource
    private MNSUtil mnsUtil;

    @Resource
    private ICommonService commonService;

    @Resource
    private MessageFlatPad messageFlatPad;

    @Autowired
    private RefundManageFeignService refundManageFeignService;

    @Autowired
    private PmsReservationOptionFeignService pmsReservationOptionFeignService;

    /**
     * 创建订单
     * @param reservationParamDto
     * @return
     * @throws Exception
     */
    @Override
    public ReservationBaseDto createReservation(ReservationParamDto reservationParamDto)throws Exception{
        ReservationBaseDto reservationBaseDto=reservationParamDto.getReservationBase();
        L2RAssert.stringBlank(reservationBaseDto.getArrtime(),"到店时间为空");
        L2RAssert.stringBlank(reservationBaseDto.getDeptime(),"离店时间为空");

        if(StringUtil.isNotBlank(reservationParamDto.getTicket())){
            MemberSessionDto memberSession= ApiThreadLocal.getMemberSessionThreadLocal();
            String memberNo=memberSession.getMemberNo();
            String openId=memberSession.getOpenid();
            String memberLevel=memberSession.getMemberLevel();
            reservationBaseDto.setMemberNo(memberNo);
            reservationBaseDto.setOpenid(openId);
            reservationParamDto.setMemberLevel(memberLevel);
        }
        ReservationDto reservationDto=reservationService.createReservation(reservationParamDto);
        reservationBaseDto=reservationDto.getReservationBaseDto();
        String lhotelGroupCode=reservationDto.getReservationBaseDto().getLhotelGroupCode();
        String lhotelCode=reservationDto.getReservationBaseDto().getLhotelCode();
        String l2rNo=reservationDto.getReservationBaseDto().getL2RNo();
        String resType=reservationDto.getReservationBaseDto().getResType();

        //消息服务参数
        ReservationParamDto reservationParamDtoNew=new ReservationParamDto();
        reservationParamDtoNew.setOperType(ReservationConstant.OPERTYPE.CREATE);
        reservationParamDtoNew.setReservationBase(reservationDto.getReservationBaseDto());
        reservationParamDtoNew.setReservationPriceDtos(ClassConverUtil.copyPropertiesListToDtoList(reservationDto.getReservationPriceDtos(),ReservationPriceDto.class, false));
        reservationParamDtoNew.setGcOrderCoupons(reservationDto.getGcOrderCouponDtos());

        //订单处理状态（库存）
        reservationService.saveReservationNmsSta(lhotelGroupCode,
                lhotelCode,
                l2rNo,
                BaseConstant.ServiceCategoryConstant.OPTIMUS,
                DefaultValue.W,
                ReservationConstant.OPERTYPE.CREATE);

        //TODO 日后区分是否需要先付款
        if(!BaseRateplanConstant.RESTYPE.PRE.equals(resType)){
            //订单处理状态（PMS）
            if(DefaultValue.T.equals(reservationBaseDto.getIsToPms())){
                reservationService.saveReservationNmsSta(lhotelGroupCode,
                        lhotelCode,
                        l2rNo,
                        BaseConstant.ServiceCategoryConstant.SOUNDWAVE,
                        DefaultValue.W,
                        ReservationConstant.OPERTYPE.CREATE);
                mnsUtil.putDelayMessageToQueue(SoundwaveMnsConstant.QUEUE_ORDER_SOUNDWAVE,JacksonUtil.beanToJson(reservationParamDtoNew),2);

            }
        }else if(BaseRateplanConstant.RESTYPE.PRE.equals(resType)){
            //订单处理状态（PMS）
            if(DefaultValue.T.equals(reservationBaseDto.getIsToPms())) {
                reservationService.saveReservationNmsSta(lhotelGroupCode,
                        lhotelCode,
                        l2rNo,
                        BaseConstant.ServiceCategoryConstant.SOUNDWAVE,
                        DefaultValue.N,
                        ReservationConstant.OPERTYPE.CREATE);
            }
        }

        if(reservationDto.getReservationPromotionDtos()!=null){
            for(ReservationPromotionDto promotion:reservationDto.getReservationPromotionDtos()){
                if(promotion.getPromotionType().equals(ReservationPromotionConstant.PROMOTIONTYPE.POINT)&&!DefaultValue.NONE.equals(promotion.getPromotionCode())){
                    //订单处理状态（积分）
                    reservationService.saveReservationNmsSta(lhotelGroupCode,
                            lhotelCode,
                            l2rNo,
                            BaseConstant.ServiceCategoryConstant.RODIMUS,
                            DefaultValue.W,
                            ReservationConstant.OPERTYPE.CREATE);

                    //去扣减积分
                    PointBaseParamDto pointBaseParamDto=new PointBaseParamDto();
                    pointBaseParamDto.setPoint(promotion.getPromotionValue());
                    pointBaseParamDto.setL2RNo(l2rNo);
                    pointBaseParamDto.setLhotelCode(lhotelCode);
                    pointBaseParamDto.setOperType(ReservationConstant.OPERTYPE.CREATE);
                    pointBaseParamDto.setPointToMoney(promotion.getAmount());
                    if(StringUtil.isNotBlank(promotion.getExtend())){
                        pointBaseParamDto.setTaCode(JacksonUtil.jsonToBean(promotion.getExtend(),L2RPromotionDto.class).getTaCode());
                    }                    pointBaseParamDto.setL2rMemberNo(reservationBaseDto.getMemberNo());
                    mnsUtil.putDelayMessageToQueue(RodimusMnsConstant.QUEUE_ORDER_POINT_RODIMUS,JacksonUtil.beanToJson(pointBaseParamDto),2);


                }
            }
        }
        mnsUtil.putMessageToQueue(OptimusMnsConstant.QUEUE_ORDER_OPTIMUS,JacksonUtil.beanToJson(reservationParamDtoNew));
        mnsUtil.putMessageToQueue(RodimusMnsConstant.QUEUE_ORDER_RODIMUS,JacksonUtil.beanToJson(reservationParamDtoNew));
        return reservationDto.getReservationBaseDto();
    }

    /**
     * 更新订单确认状态
     * @param reservationNmsStaInsideParamDto
     * @throws Exception
     */
    @Override
    public void updateReservationNmsSta(ReservationNmsStaInsideParamDto reservationNmsStaInsideParamDto)throws Exception{
        ReservationNmsSta qrns=new ReservationNmsSta();
        qrns.setLhotelGroupCode(reservationNmsStaInsideParamDto.getLhotelGroupCode());
        qrns.setLhotelCode(reservationNmsStaInsideParamDto.getLhotelCode());
        qrns.setL2RNo(reservationNmsStaInsideParamDto.getL2RNo());
        qrns.setItem(reservationNmsStaInsideParamDto.getItem());
        qrns.setOperType(reservationNmsStaInsideParamDto.getOperType());
        ReservationNmsSta rns=commonService.findOneEQ(ReservationNmsSta.class,qrns);
        if(rns!=null) {
            if (StringUtil.isNotBlank(reservationNmsStaInsideParamDto.getOperSta())&&!DefaultValue.W.equals(reservationNmsStaInsideParamDto.getOperSta())) {
                rns.setOperSta(reservationNmsStaInsideParamDto.getOperSta());
                commonService.merge(rns);
                ReservationParamDto reservationParamDto=new ReservationParamDto();
                reservationParamDto.setLhotelGroupCode(reservationNmsStaInsideParamDto.getLhotelGroupCode());
                reservationParamDto.setLhotelCode(reservationNmsStaInsideParamDto.getLhotelCode());
                reservationParamDto.setL2rNo(reservationNmsStaInsideParamDto.getL2RNo());
                reservationParamDto.setTaNo(reservationNmsStaInsideParamDto.getTaNo());
                if(ReservationConstant.OPERTYPE.CREATE.equals(reservationNmsStaInsideParamDto.getOperType())){
                    reservationParamDto.setConfirmSta(ReservationConstant.CONFIRMSTA.NOTCFMD);
                    mnsUtil.putMessageToQueue(JetfireMnsConstant.QUEUE_ORDER_CONFIRMSTA,JacksonUtil.beanToJson(reservationParamDto));
                }
                if(ReservationConstant.OPERTYPE.CANCEL.equals(reservationNmsStaInsideParamDto.getOperType())){
                    reservationParamDto.setConfirmSta(ReservationConstant.CONFIRMSTA.CANCELING);
                    mnsUtil.putMessageToQueue(JetfireMnsConstant.QUEUE_ORDER_CONFIRMSTA,JacksonUtil.beanToJson(reservationParamDto));
                }
                if(ReservationConstant.OPERTYPE.REVERT.equals(reservationNmsStaInsideParamDto.getOperType())){
                    reservationParamDto.setConfirmSta(ReservationConstant.CONFIRMSTA.REVERTING);
                    mnsUtil.putMessageToQueue(JetfireMnsConstant.QUEUE_ORDER_CONFIRMSTA,JacksonUtil.beanToJson(reservationParamDto));
                }
            }
        }
    }

    /**
     * 更新订单状态
     * @param reservationBaseInsideParamDto
     * @throws Exception
     */
    @Override
    public void updateReservationBase(ReservationBaseInsideParamDto reservationBaseInsideParamDto)throws Exception{
        ReservationBase queryrb=new ReservationBase();
        queryrb.setLhotelGroupCode(reservationBaseInsideParamDto.getLhotelGroupCode());
        queryrb.setLhotelCode(reservationBaseInsideParamDto.getLhotelCode());
        queryrb.setL2RNo(reservationBaseInsideParamDto.getL2RNo());
        ReservationBase rb=commonService.findOneEQ(ReservationBase.class,queryrb);
        if(rb!=null){
            if(StringUtil.isNotBlank(reservationBaseInsideParamDto.getResSta())){
                rb.setResSta(reservationBaseInsideParamDto.getResSta());
            }
            if(StringUtil.isNotBlank(reservationBaseInsideParamDto.getCrsNo())){
                rb.setCrsNo(reservationBaseInsideParamDto.getCrsNo());
            }
            commonService.merge(rb);
        }else {
            throw new JetfireException(BaseSysExceptionEnum.DATA_IS_NOT_EXIST,"lhotelGroupCode:"+reservationBaseInsideParamDto.getLhotelGroupCode() +"lhotelCode:"+reservationBaseInsideParamDto.getLhotelCode() +"l2rNo:"+reservationBaseInsideParamDto.getL2RNo());
        }
    }

    /**
     * 支付入账
     * @param reservationPayInsideParamDto
     * @throws Exception
     */
    @Override
    public void saveReservationPay(ReservationPayInsideParamDto reservationPayInsideParamDto)throws Exception{
        String lhotelGroupCode=reservationPayInsideParamDto.getLhotelGroupCode();
        String lhotelCode=reservationPayInsideParamDto.getLhotelCode();
        String l2rNo=reservationPayInsideParamDto.getOrderNo();
        ReservationPayment queryrp=new ReservationPayment();
        queryrp.setLhotelGroupCode(lhotelGroupCode);
        queryrp.setLhotelCode(lhotelCode);
        queryrp.setTaNo(reservationPayInsideParamDto.getTaNo());
        QueryResult<ReservationPayment> reservationPaymentResult = commonService.findAllEQ(ReservationPayment.class, queryrp);
        if(reservationPaymentResult.getResultlist()==null||reservationPaymentResult.getResultlist().size()==0){
            ReservationBase queryrb=new ReservationBase();
            queryrb.setLhotelGroupCode(lhotelGroupCode);
            queryrb.setLhotelCode(lhotelCode);
            queryrb.setL2RNo(l2rNo);
            ReservationBase rb= commonService.findOneEQ(ReservationBase.class, queryrb);

            ReservationAccount queryRA=new ReservationAccount();
            queryRA.setLhotelGroupCode(lhotelGroupCode);
            queryRA.setLhotelCode(lhotelCode);
            queryRA.setL2RNo(l2rNo);
            ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryRA);

            reservationAccount.setDeposit(reservationAccount.getDeposit().add(reservationPayInsideParamDto.getAmount()));
            if(rb.getAmount().compareTo(reservationAccount.getDeposit().add(reservationAccount.getHotelPromotion()))==0) {
                reservationAccount.setL2rPaysta(ReservationConstant.L2RPAYSTA.DONE);
            }
            if(!Objects.equals(reservationPayInsideParamDto.getIsSavePmsPay(),DefaultValue.T)) {
                reservationAccount.setPmsPaysta(ReservationConstant.PMSPAYSTA.DONE);
            }
            reservationAccount.setL2RNo(l2rNo);
            commonService.merge(reservationAccount);

            PmsReservationOptionParamDto pmsReservationOptionParamDto=new PmsReservationOptionParamDto();
            pmsReservationOptionParamDto.setPbChannel(rb.getPbChannel());
            pmsReservationOptionParamDto.setResType(rb.getResType());
            PmsReservationOptionDto pmsReservationOptionDto= pmsReservationOptionFeignService.getPmsReservationOption(pmsReservationOptionParamDto.paramToMap()).getResultInfo();
            ReservationPayment reservationPayment=ClassConverUtil.copyProperties(reservationPayInsideParamDto,ReservationPayment.class);
            reservationPayment.setResDate(new Date());
            reservationPayment.setMcCode(rb.getMcCode());
            if(Objects.equals(reservationPayInsideParamDto.getIsSavePmsPay(),DefaultValue.T)){
                reservationPayment.setPmsPaysta(ReservationConstant.PMSPAYSTA.NONE);
            }else {
                reservationPayment.setPmsPaysta(ReservationConstant.PMSPAYSTA.DONE);
            }
            reservationPayment.setCheckSta(ReservationConstant.CHECKSTA.NONE);
            reservationPayment.setL2rPaysta(ReservationConstant.L2RPAYSTA.DONE);
            reservationPayment.setL2RNo(reservationPayInsideParamDto.getOrderNo());
            String taCode=null;
            if(StringUtil.isNotBlank(reservationPayInsideParamDto.getTaCode())){
                taCode=reservationPayInsideParamDto.getTaCode();
            }else {
                if(pmsReservationOptionDto.getTaCodeMap()!=null){
                    taCode=pmsReservationOptionDto.getTaCodeMap().get(reservationPayInsideParamDto.getPayType());
                }
            }
            reservationPayment.setTaCode(taCode);
            commonService.save(reservationPayment);
            if(Objects.equals(DefaultValue.T,rb.getIsToPms())&&Objects.equals(reservationPayInsideParamDto.getIsSavePmsPay(),DefaultValue.T)) {
                ReservationParamDto reservationParamDto = new ReservationParamDto();
                reservationParamDto.setLhotelCode(lhotelCode);
                reservationParamDto.setL2rNo(l2rNo);
                reservationParamDto.setReservationPaymentDto(ClassConverUtil.copyPropertiesToDto(reservationPayment, ReservationPaymentDto.class, false));
                mnsUtil.putDelayMessageToQueue(JetfireMnsConstant.QUEUE_ORDER_SAVEPAY, JacksonUtil.beanToJson(reservationParamDto), 2);
            }
        }
    }

    @Override
    public boolean savePayToPms(ReservationParamDto reservationParamDto)throws Exception{
        ReservationBase queryRb=new ReservationBase();
        queryRb.setLhotelGroupCode(reservationParamDto.getLhotelGroupCode());
        queryRb.setLhotelCode(reservationParamDto.getLhotelCode());
        queryRb.setL2RNo(reservationParamDto.getL2rNo());
        ReservationBase rb = commonService.findOneEQ(ReservationBase.class, queryRb);
        if(ReservationConstant.CONFIRMSTA.AUTOCFMD.equals(rb.getConfirmSta())){
            ReservationPaymentDto reservationPaymentDto=reservationParamDto.getReservationPaymentDto();
            PmsReservationOptionParamDto pmsReservationOptionParamDto=new PmsReservationOptionParamDto();
            pmsReservationOptionParamDto.setPbChannel(rb.getPbChannel());
            pmsReservationOptionParamDto.setResType(rb.getResType());
            PmsReservationOptionDto pmsReservationOptionDto= pmsReservationOptionFeignService.getPmsReservationOption(pmsReservationOptionParamDto.paramToMap()).getResultInfo();
            L2RAssert.objectNull(pmsReservationOptionDto,"预定必备参数不存在");
            reservationService.saveReservationPayToPms(rb.getLhotelGroupCode(),rb.getLhotelCode(),rb.getL2RNo(),pmsReservationOptionDto.getPmsResType(),reservationPaymentDto);
            return true;
        }else if(ReservationConstant.CONFIRMSTA.NOTCFMD.equals(rb.getConfirmSta())) {
            return false;
        }else {
            throw new JetfireException(JetfireSysExceptionEnum.RESER_SAVE_PAY_ERROR,JacksonUtil.beanToJson(reservationParamDto));
        }
    }

    /**
     * 定时处理订单确认状态
     * @param reservationParamDto
     * @throws Exception
     */
    @Override
    public boolean updateReservationConfirmSta(ReservationParamDto reservationParamDto)throws Exception{
        ReservationBase queryRB=new ReservationBase();
        queryRB.setLhotelGroupCode(reservationParamDto.getLhotelGroupCode());
        queryRB.setLhotelCode(reservationParamDto.getLhotelCode());
        queryRB.setL2RNo(reservationParamDto.getL2rNo());
        ReservationBase rb = commonService.findOneEQ(ReservationBase.class, queryRB);
        if(rb!=null){
            if(rb.getConfirmSta().equals(reservationParamDto.getConfirmSta())){
                switch (reservationParamDto.getConfirmSta()){
                    case ReservationConstant.CONFIRMSTA.NOTCFMD:
                        return this.excuteReservationNmsStaWhenNOTCFMD(rb);
                    case ReservationConstant.CONFIRMSTA.REVERTING:
                        this.excuteReservationNmsStaWhenREVERTING(rb,reservationParamDto.getTaNo());
                        return true;
                    case ReservationConstant.CONFIRMSTA.CANCELING:
                        this.excuteReservationNmsStaWhenCANCELING(rb);
                        return true;
                    default:
                        return true;
                }
            }else {
                return false;
            }
        }else{
            throw new JetfireException(JetfireSysExceptionEnum.RESER_IS_NOT_EXIST);
        }
    }

    /**
     * 未确认订单判断处理
     * @param reservationBase
     * @throws Exception
     */
    private boolean excuteReservationNmsStaWhenNOTCFMD(ReservationBase reservationBase) throws Exception {
        String lhotelGroupCode=reservationBase.getLhotelGroupCode();
        String lhotelCode=reservationBase.getLhotelCode();
        String l2rNo=reservationBase.getL2RNo();
        boolean flag=true;
        ReservationNmsSta queryRNS=new ReservationNmsSta();
        queryRNS.setLhotelGroupCode(lhotelGroupCode);
        queryRNS.setLhotelCode(lhotelCode);
        queryRNS.setL2RNo(l2rNo);
        queryRNS.setOperType(ReservationConstant.OPERTYPE.CREATE);
        QueryResult<ReservationNmsSta> rnsResult = commonService.findAllEQ(ReservationNmsSta.class, queryRNS);
        int successNum=0;
        int failNum=0;
        if(rnsResult.getResultlist()!=null&&rnsResult.getResultlist().size()>0){
            List<ReservationNmsSta> successRnsList=new ArrayList<>();
            for(ReservationNmsSta rns:rnsResult.getResultlist()){
                if(DefaultValue.T.equals(rns.getOperSta())||DefaultValue.O.equals(rns.getOperSta())){
                    successNum++;
                    if(DefaultValue.T.equals(rns.getOperSta())){
                        successRnsList.add(rns);
                    }
                }else if(DefaultValue.F.equals(rns.getOperSta())){
                    failNum++;
                }
                //判断PMS订单服务未发起时，已付金额与订单金额一致则下发PMS订单
                if(DefaultValue.N.equals(rns.getOperSta())&&BaseConstant.ServiceCategoryConstant.SOUNDWAVE.equals(rns.getItem())){
                    ReservationAccount queryRA=new ReservationAccount();
                    queryRA.setLhotelGroupCode(lhotelGroupCode);
                    queryRA.setLhotelCode(lhotelCode);
                    queryRA.setL2RNo(l2rNo);
                    ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryRA);
                    if(ReservationConstant.L2RPAYSTA.DONE.equals(reservationAccount.getL2rPaysta())){
                        ReservationPrice queryPrice=new ReservationPrice();
                        queryPrice.setLhotelGroupCode(lhotelGroupCode);
                        queryPrice.setLhotelCode(lhotelCode);
                        queryPrice.setL2RNo(l2rNo);
                        QueryResult<ReservationPrice> priceResult = commonService.findAllEQ(ReservationPrice.class, queryPrice);
                        List<ReservationPriceDto> reservationPriceDtos = ClassConverUtil.copyPropertiesListToDtoList(priceResult.getResultlist(), ReservationPriceDto.class, false);

                        GcOrderCoupon queryCoupon=new GcOrderCoupon();
                        queryCoupon.setLhotelGroupCode(lhotelGroupCode);
                        queryCoupon.setLhotelCode(lhotelCode);
                        queryCoupon.setL2RNo(l2rNo);
                        QueryResult<GcOrderCoupon> couponResult = commonService.findAllEQ(GcOrderCoupon.class, queryCoupon);
                        List<GcOrderCouponDto> gcOrderCouponDtos = ClassConverUtil.copyPropertiesListToDtoList(couponResult.getResultlist(), GcOrderCouponDto.class, false);


                        ReservationParamDto reservationParamDto=new ReservationParamDto();
                        reservationParamDto.setOperType(ReservationConstant.OPERTYPE.CREATE);
                        reservationParamDto.setReservationBase(ClassConverUtil.copyPropertiesToDto(reservationBase,ReservationBaseDto.class, false));
                        reservationParamDto.setReservationPriceDtos(reservationPriceDtos);
                        reservationParamDto.setGcOrderCoupons(gcOrderCouponDtos);
                        mnsUtil.putMessageToQueue(SoundwaveMnsConstant.QUEUE_ORDER_SOUNDWAVE,JacksonUtil.beanToJson(reservationParamDto));
                    }else {
                        flag=false;
                    }
                }
            }
            if(successNum==rnsResult.getTotalrecord()){
                reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.AUTOCFMD);
                Map<String,String> map=new HashMap<>();
                map.put("lhotelGroupCode",reservationBase.getLhotelGroupCode());
                map.put("lhotelCode",reservationBase.getLhotelCode());
                map.put("hotelName",reservationBase.getHotelName());
                map.put("crsNo",reservationBase.getCrsNo());
                map.put("l2rNo",reservationBase.getL2RNo());
                map.put("rsvMan",reservationBase.getRsvMan());
                map.put("arrtime", DateUtil.format(reservationBase.getArr(),DateUtil.newFormat));
                map.put("deptime",DateUtil.format(reservationBase.getDep(),DateUtil.newFormat));
                map.put("roomNum",reservationBase.getRoomNum().toString());
                map.put("mcdName",reservationBase.getMcdName());
                map.put("amount",reservationBase.getAmount().toString());
                map.put("openid",reservationBase.getOpenid());
                messageFlatPad.send(reservationBase.getLhotelGroupCode(), null, MsgConstant.TEMPLET_CODE.ORDER_SUCCESS, map);
            }
            //有失败内容且失败跟成功次数的总和等于总条数（没有处理中或者未处理内容）
            if(failNum>0&&(failNum+successNum)==rnsResult.getTotalrecord()){
                reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.REVERTING);
                for(ReservationNmsSta rns:successRnsList){
                    this.revertReservation(lhotelGroupCode,lhotelCode,l2rNo,rns,reservationBase);
                }
            }
            commonService.merge(reservationBase);
        }
        return  flag;
    }


    /**
     * 撤销中订单判断处理
     * @param reservationBase
     * @throws Exception
     */
    private void excuteReservationNmsStaWhenREVERTING(ReservationBase reservationBase,String taNo) throws Exception {
        ReservationNmsSta queryRNS=new ReservationNmsSta();
        queryRNS.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
        queryRNS.setLhotelCode(reservationBase.getLhotelCode());
        queryRNS.setL2RNo(reservationBase.getL2RNo());
        queryRNS.setOperType(ReservationConstant.OPERTYPE.REVERT);
        QueryResult<ReservationNmsSta> rnsResult = commonService.findAllEQ(ReservationNmsSta.class, queryRNS);
        int successNum=0;
        int failNum=0;
        if(rnsResult.getResultlist()!=null&&rnsResult.getResultlist().size()>0){
            for(ReservationNmsSta rns:rnsResult.getResultlist()){
                if(DefaultValue.T.equals(rns.getOperSta())){
                    successNum++;
                }else if(DefaultValue.F.equals(rns.getOperSta())){
                    failNum++;
                }
            }
            if(successNum==rnsResult.getTotalrecord()){
                reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.REVERTCFMD);
                Map<String,String> map=new HashMap<>();
                map.put("lhotelGroupCode",reservationBase.getLhotelGroupCode());
                map.put("lhotelCode",reservationBase.getLhotelCode());
                map.put("hotelName",reservationBase.getHotelName());
                map.put("arrtime", DateUtil.format(reservationBase.getArr(),DateUtil.newFormat));
                map.put("deptime",DateUtil.format(reservationBase.getDep(),DateUtil.newFormat));
                map.put("roomNum",reservationBase.getRoomNum().toString());
                map.put("mcdName",reservationBase.getMcdName());
                map.put("openid",reservationBase.getOpenid());
                if(StringUtil.isNotBlank(reservationBase.getCrsNo())){
                    map.put("crsNo",reservationBase.getCrsNo());
                }else {
                    map.put("crsNo","");
                }
                map.put("l2rNo",reservationBase.getL2RNo());
                messageFlatPad.send(reservationBase.getLhotelGroupCode(), null, MsgConstant.TEMPLET_CODE.CANCEL, map);
                if(StringUtil.isNotBlank(taNo)){
                    ReservationPayment queryPayment=new ReservationPayment();
                    queryPayment.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
                    queryPayment.setLhotelCode(reservationBase.getLhotelCode());
                    queryPayment.setL2RNo(reservationBase.getL2RNo());
                    queryPayment.setTaNo(taNo);
                    ReservationPayment reservationPayment=commonService.findOneEQ(ReservationPayment.class,queryPayment);
                    reservationPayment.setL2rPaysta(ReservationConstant.L2RPAYSTA.REJECT);
                    commonService.merge(reservationPayment);
                    ReservationAccount queryAccount=new ReservationAccount();
                    queryAccount.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
                    queryAccount.setLhotelCode(reservationBase.getLhotelCode());
                    queryAccount.setL2RNo(reservationBase.getL2RNo());
                    ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryAccount);
                    reservationAccount.setDeposit(reservationAccount.getDeposit().subtract(reservationPayment.getAmount()));
                    if(reservationAccount.getDeposit().compareTo(new BigDecimal(0))==0){
                        reservationAccount.setL2rPaysta(ReservationConstant.L2RPAYSTA.REJECT);
                    }
                    commonService.merge(reservationAccount);
                }
            }
            if(failNum>0){
                reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.REVERTREJ);
            }
            commonService.merge(reservationBase);
        }
    }

    /**
     * 取消中订单判断处理
     * @param reservationBase
     * @throws Exception
     */
    private void excuteReservationNmsStaWhenCANCELING(ReservationBase reservationBase) throws Exception {
        ReservationNmsSta queryRNS=new ReservationNmsSta();
        queryRNS.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
        queryRNS.setLhotelCode(reservationBase.getLhotelCode());
        queryRNS.setL2RNo(reservationBase.getL2RNo());
        queryRNS.setOperType(ReservationConstant.OPERTYPE.CANCEL);
        QueryResult<ReservationNmsSta> rnsResult = commonService.findAllEQ(ReservationNmsSta.class, queryRNS);
        int successNum=0;
        int failNum=0;
        if(rnsResult.getResultlist()!=null&&rnsResult.getResultlist().size()>0){
            for(ReservationNmsSta rns:rnsResult.getResultlist()){
                if(DefaultValue.T.equals(rns.getOperSta())){
                    successNum++;
                }else if(DefaultValue.F.equals(rns.getOperSta())){
                    failNum++;
                }
            }
            if(successNum==rnsResult.getTotalrecord()){
                reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.CANCELCFMD);
                Map<String,String> map=new HashMap<>();
                map.put("lhotelGroupCode",reservationBase.getLhotelGroupCode());
                map.put("lhotelCode",reservationBase.getLhotelCode());
                map.put("hotelName",reservationBase.getHotelName());
                map.put("hotelName",reservationBase.getHotelName());
                map.put("arrtime", DateUtil.format(reservationBase.getArr(),DateUtil.newFormat));
                map.put("deptime",DateUtil.format(reservationBase.getDep(),DateUtil.newFormat));
                map.put("roomNum",reservationBase.getRoomNum().toString());
                map.put("mcdName",reservationBase.getMcdName());
                map.put("openid",reservationBase.getOpenid());
                if(StringUtil.isNotBlank(reservationBase.getCrsNo())){
                    map.put("crsNo",reservationBase.getCrsNo());
                }else {
                    map.put("crsNo","");
                }
                map.put("l2rNo",reservationBase.getL2RNo());
                messageFlatPad.send(reservationBase.getLhotelGroupCode(), null, MsgConstant.TEMPLET_CODE.CANCEL, map);
                if(!BaseRateplanConstant.CANCELTYPE.BAN.equals(reservationBase.getCancelType())){
                    //发起退款申请
                    this.requestRefund(reservationBase);
                }
            }
            if(failNum>0){
                reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.CANCELREJ);
            }
            commonService.merge(reservationBase);
        }
    }

   /* *//**
     * 入账中订单判断处理
     * @param reservationBase
     * @throws Exception
     *//*
    private void excuteReservationNmsStaWhenSAVEPAYING(ReservationBase reservationBase,List<ReservationPaymentDto> reservationPaymentDtos) throws Exception {
        try {
            for(ReservationPaymentDto reservationPaymentDto:reservationPaymentDtos){
                reservationService.saveReservationPayToPms(reservationBase.getLhotelGroupCode(),reservationBase.getLhotelCode(),reservationBase.getL2RNo(),reservationPaymentDto.getPayChannel());
            }
            reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.AUTOCFMD);
            messageFlatPad.send(reservationBase.getLhotelGroupCode(), null, MsgConstant.TEMPLET_CODE.ORDER_SUCCESS, ClassConverUtil.copyPropertiesToDto(reservationBase,ReservationBaseDto.class, false));
        }catch (Exception e){
            reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.SAVEPAYREJ);
            throw new JetfireException(JetfireSysExceptionEnum.SAVE_PMS_PAY_ERROR);
        }finally {
            commonService.merge(reservationBase);
        }
    }*/

    /**
     * 发起撤销订单消息
     * @param lhotelGroupCode
     * @param lhotelCode
     * @param l2rNo
     * @param successRns
     * @param reservationBase
     * @throws Exception
     */
    private void revertReservation(String lhotelGroupCode,String lhotelCode,String l2rNo,
                                   ReservationNmsSta successRns,ReservationBase reservationBase) throws Exception {
        ReservationNmsSta reservationNmsSta=new ReservationNmsSta();
        reservationNmsSta.setLhotelGroupCode(lhotelGroupCode);
        reservationNmsSta.setLhotelCode(lhotelCode);
        reservationNmsSta.setL2RNo(l2rNo);
        reservationNmsSta.setItem(successRns.getItem());
        reservationNmsSta.setOperType(ReservationConstant.OPERTYPE.REVERT);
        ReservationNmsSta rns = commonService.findOneEQ(ReservationNmsSta.class, reservationNmsSta);
        if(rns==null){
            commonService.save(reservationNmsSta);
        }
        ReservationParamDto reservationParamDto = new ReservationParamDto();
        reservationParamDto.setOperType(ReservationConstant.OPERTYPE.REVERT);
        reservationParamDto.setReservationBase(ClassConverUtil.copyPropertiesToDto(reservationBase, ReservationBaseDto.class, false));
        switch (successRns.getItem()){
            case BaseConstant.ServiceCategoryConstant.SOUNDWAVE:
                mnsUtil.putMessageToQueue(SoundwaveMnsConstant.QUEUE_ORDER_SOUNDWAVE,JacksonUtil.beanToJson(reservationParamDto));
                return;
            case BaseConstant.ServiceCategoryConstant.OPTIMUS:
                mnsUtil.putMessageToQueue(OptimusMnsConstant.QUEUE_ORDER_OPTIMUS,JacksonUtil.beanToJson(reservationParamDto));
                return;
            case BaseConstant.ServiceCategoryConstant.TRAILBREAKER:
                mnsUtil.putMessageToQueue(TrailbreakerMnsConstant.QUEUE_ORDER_TRAILBREAKER,JacksonUtil.beanToJson(reservationParamDto));
                return;
            case BaseConstant.ServiceCategoryConstant.RODIMUS:
                ReservationPromotion queryRE=new ReservationPromotion();
                queryRE.setLhotelGroupCode(lhotelGroupCode);
                queryRE.setLhotelCode(lhotelCode);
                queryRE.setL2RNo(l2rNo);
                QueryResult<ReservationPromotion> reservationPromotionQueryResult=commonService.findAllEQ(ReservationPromotion.class,queryRE);
                if(reservationPromotionQueryResult.getResultlist()!=null){
                    for(ReservationPromotion l2rPromotion:reservationPromotionQueryResult.getResultlist()){
                        if(ReservationConstant.PromotionType.POINT.equals(l2rPromotion.getPromotionType())&&!DefaultValue.NONE.equals(l2rPromotion.getPromotionCode())){
                            PointBaseParamDto pointBaseParamDto=new PointBaseParamDto();
                            pointBaseParamDto.setPoint(l2rPromotion.getPromotionValue());
                            pointBaseParamDto.setL2RNo(l2rNo);
                            pointBaseParamDto.setLhotelCode(lhotelCode);
                            pointBaseParamDto.setOperType(ReservationConstant.OPERTYPE.REVERT);
                            pointBaseParamDto.setPointToMoney(l2rPromotion.getAmount());
                            pointBaseParamDto.setL2rMemberNo(reservationBase.getMemberNo());
                            if(StringUtil.isNotBlank(l2rPromotion.getExtend())){
                                pointBaseParamDto.setTaCode(JacksonUtil.jsonToBean(l2rPromotion.getExtend(),L2RPromotionDto.class).getTaCode());
                            }
                            mnsUtil.putMessageToQueue(RodimusMnsConstant.QUEUE_ORDER_POINT_RODIMUS,JacksonUtil.beanToJson(pointBaseParamDto));
                        }
                    }
                }
                return;
            default:
                return;
        }
    }


    @Override
    public void cancelReservationBase(ReservationParamDto reservationParamDto)throws Exception {
        ReservationBase queryrb=new ReservationBase();
        queryrb.setLhotelGroupCode(reservationParamDto.getLhotelGroupCode());
        queryrb.setLhotelCode(reservationParamDto.getLhotelCode());
        queryrb.setL2RNo(reservationParamDto.getL2rNo());
        queryrb.setTerminalNo(reservationParamDto.getTerminalNo());
        queryrb.setResSta(ReservationConstant.RESSTA.R);
        QueryResult<ReservationBase> rbResult = commonService.findAllEQ(ReservationBase.class, queryrb);
        if (rbResult.getResultlist() != null && rbResult.getResultlist().size() == 1) {
            ReservationBase rb = rbResult.getResultlist().get(0);
            if(rb.getConfirmSta().equals(ReservationConstant.CONFIRMSTA.AUTOCFMD)){
                reservationService.cancelReservation(rb);
            }
        }else {
            throw new JetfireException(BaseSysExceptionEnum.DATA_IS_NOT_EXIST,reservationParamDto.toString());
        }
    }

    @Override
    public ReservationDto findReservation(ReservationParamDto reservationInsideParamDto) throws Exception {
        ReservationBase queryrb=new ReservationBase();
        queryrb.setLhotelGroupCode(reservationInsideParamDto.getLhotelGroupCode());
        queryrb.setLhotelCode(reservationInsideParamDto.getLhotelCode());
        queryrb.setL2RNo(reservationInsideParamDto.getL2rNo());
        queryrb.setTerminalNo(reservationInsideParamDto.getTerminalNo());
        queryrb.setResSta(reservationInsideParamDto.getResSta());
        queryrb.setConfirmSta(reservationInsideParamDto.getConfirmSta());
        QueryResult<ReservationBase> rbResult = commonService.findAllEQ(ReservationBase.class, queryrb);
        if(rbResult.getResultlist()!=null&&rbResult.getResultlist().size()>0){
            ReservationBase rb = rbResult.getResultlist().get(0);
            ReservationDto reservationDto=new ReservationDto();
            ReservationPrice queryrp=new ReservationPrice();
            queryrp.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryrp.setLhotelCode(rb.getLhotelCode());
            queryrp.setL2RNo(rb.getL2RNo());
            HashMap<String,String> orderBy=new HashMap<>();
            orderBy.put(ReservationPrice.ReservationPrice_.resDate.toString(),"ASC");
            QueryResult<ReservationPrice> reservationPriceResult = commonService.findAllEQ(ReservationPrice.class, queryrp, null, null, orderBy, null);
            List<ReservationPrice> reservationPriceList = reservationPriceResult.getResultlist();
            reservationDto.setReservationBaseDto(ClassConverUtil.copyPropertiesToDto(rb,ReservationBaseDto.class, false));
            if(reservationPriceList!=null&&reservationPriceList.size()>0){
                List<ReservationPriceDto> reservationPriceDtoList=new ArrayList<>();
                for (ReservationPrice rp:reservationPriceList){
                    if(rb.getL2RNo().equals(rp.getL2RNo())){
                        ReservationPriceDto reservationPriceDto=ClassConverUtil.copyPropertiesToDto(rp,ReservationPriceDto.class, false);
                        reservationPriceDtoList.add(reservationPriceDto);
                    }
                }
                reservationDto.setReservationPriceDtos(reservationPriceDtoList);
            }
            ReservationAccount queryRA=new ReservationAccount();
            queryRA.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryRA.setLhotelCode(rb.getLhotelCode());
            queryRA.setL2RNo(rb.getL2RNo());
            ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryRA);
            reservationDto.setReservationAccountDto(ClassConverUtil.copyPropertiesToDto(reservationAccount, ReservationAccountDto.class,false));
            ReservationPayment queryRP=new ReservationPayment();
            queryRP.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryRP.setLhotelCode(rb.getLhotelCode());
            queryRP.setL2RNo(rb.getL2RNo());
            queryRP.setPayChannel(reservationInsideParamDto.getPayChannel());
            queryRP.setPayType(reservationInsideParamDto.getPayType());
            queryRP.setPmsPaysta(reservationInsideParamDto.getPmsPaysta());
            queryRP.setL2rPaysta(reservationInsideParamDto.getL2rPaysta());
            QueryResult<ReservationPayment> reservationPaymentResult = commonService.findAllEQ(ReservationPayment.class, queryRP);
            reservationDto.setReservationPaymentDtos(ClassConverUtil.copyPropertiesList(reservationPaymentResult.getResultlist(),ReservationPaymentDto.class));
            return reservationDto;
        }else {
            throw new JetfireException(JetfireSysExceptionEnum.RESER_IS_NOT_EXIST);
        }
    }


    /**
     * 获取订单列表
     * @param reservationParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<ReservationBaseDto> listReservations(ReservationManagerParamDto reservationParamDto)throws Exception{
        ReservationBase queryrb=new ReservationBase();
        queryrb.setLhotelGroupCode(reservationParamDto.getLhotelGroupCode());
        queryrb.setLhotelCode(reservationParamDto.getLhotelCode());
        queryrb.setL2RNo(reservationParamDto.getL2rNo());
        queryrb.setTerminalNo(reservationParamDto.getTerminalNo());
        queryrb.setResSta(reservationParamDto.getResSta());
        queryrb.setConfirmSta(reservationParamDto.getConfirmSta());
        queryrb.setCrsNo(reservationParamDto.getCrsNo());
        queryrb.setCreateDatetime(reservationParamDto.getCreateTime());
        queryrb.setArr(reservationParamDto.getArr());
        queryrb.setDep(reservationParamDto.getDep());
        HashMap<String,String> orderBy=Optional.ofNullable(reservationParamDto.getSortBy()).map(soryBy->{
            HashMap<String,String> order=new HashMap();
            order.put(reservationParamDto.getSortBy(),reservationParamDto.getOrder());
            return order;
        }).orElse(null);
        QueryResult<ReservationBase> rbResult = commonService.findAllEQ(ReservationBase.class, queryrb,reservationParamDto.getCurrentPage(),reservationParamDto.getPageSize(),orderBy,null);
        List<ReservationBaseDto> reservationBaseDtos=Optional.ofNullable(rbResult.getResultlist()).map(list->list.stream().map(
                TryExceptionUtil.of(event->{
                    ReservationBaseDto reservationBaseDto=ClassConverUtil.copyPropertiesToDto(event,ReservationBaseDto.class,false);
                    reservationBaseDto.setCreateTime(event.getCreateDatetime());
                    reservationBaseDto.setResStaDes(ReservationResStaEnum.getDesByCode(reservationBaseDto.getResSta()));
                    reservationBaseDto.setConfirmStaDes(ReservationConfirmStaEnum.getDesByCode(reservationBaseDto.getConfirmSta()));
                    reservationBaseDto.setResTypeDes(ReservationResTypeEnum.getDesByCode(reservationBaseDto.getResType()));
                    reservationBaseDto.setPbChannelDes(ReservationPbChannelEnum.getDesByCode(reservationBaseDto.getPbChannel()));
                    return reservationBaseDto;
                })
        ).collect(Collectors.toList())).orElse(null);
        QueryResult<ReservationBaseDto> reservationBaseDtoQueryResult=new QueryResult<>();
        reservationBaseDtoQueryResult.setCurrentPage(rbResult.getCurrentPage());
        reservationBaseDtoQueryResult.setTotalrecord(rbResult.getTotalrecord());
        reservationBaseDtoQueryResult.setResultlist(reservationBaseDtos);
        return reservationBaseDtoQueryResult;
    }

    /**
     * 获取订单详情
     * @param reservationParamDto
     * @return
     * @throws Exception
     */
    @Override
    public ReservationDto getReservation(ReservationParamDto reservationParamDto)throws Exception{
        ReservationBase rb= commonService.findOneById(ReservationBase.class,reservationParamDto.getId());
        if(rb!=null){
            ReservationDto reservationDto=new ReservationDto();
            ReservationPrice queryrp=new ReservationPrice();
            queryrp.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryrp.setLhotelCode(rb.getLhotelCode());
            queryrp.setL2RNo(rb.getL2RNo());
            HashMap<String,String> orderBy=new HashMap<>();
            orderBy.put(ReservationPrice.ReservationPrice_.resDate.toString(),"ASC");
            QueryResult<ReservationPrice> reservationPriceResult = commonService.findAllEQ(ReservationPrice.class, queryrp, null, null, orderBy, null);
            List<ReservationPrice> reservationPriceList = reservationPriceResult.getResultlist();
            reservationDto.setReservationBaseDto(ClassConverUtil.copyPropertiesToDto(rb,ReservationBaseDto.class, false));
            if(reservationPriceList!=null&&reservationPriceList.size()>0){
                List<ReservationPriceDto> reservationPriceDtoList=new ArrayList<>();
                for (ReservationPrice rp:reservationPriceList){
                    if(rb.getL2RNo().equals(rp.getL2RNo())){
                        ReservationPriceDto reservationPriceDto=ClassConverUtil.copyPropertiesToDto(rp,ReservationPriceDto.class, false);
                        reservationPriceDtoList.add(reservationPriceDto);
                    }
                }
                reservationDto.setReservationPriceDtos(reservationPriceDtoList);
            }
            ReservationAccount queryRA=new ReservationAccount();
            queryRA.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryRA.setLhotelCode(rb.getLhotelCode());
            queryRA.setL2RNo(rb.getL2RNo());
            ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryRA);
            ReservationPromotion queryPromotion=new ReservationPromotion();
            queryPromotion.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryPromotion.setLhotelCode(rb.getLhotelCode());
            queryPromotion.setL2RNo(rb.getL2RNo());
            QueryResult<ReservationPromotion> reservationPromotionQueryResult = commonService.findAllEQ(ReservationPromotion.class, queryPromotion);
            ReservationPayment queryPayment=new ReservationPayment();
            queryPayment.setLhotelGroupCode(rb.getLhotelGroupCode());
            queryPayment.setLhotelCode(rb.getLhotelCode());
            queryPayment.setL2RNo(rb.getL2RNo());
            QueryResult<ReservationPayment> reservationPaymentQueryResult = commonService.findAllEQ(ReservationPayment.class, queryPayment);
            reservationDto.setReservationPromotionDtos(ClassConverUtil.copyPropertiesListToDtoList(reservationPromotionQueryResult.getResultlist(),ReservationPromotionDto.class,false));
            reservationDto.setReservationPaymentDtos(ClassConverUtil.copyPropertiesListToDtoList(reservationPaymentQueryResult.getResultlist(),ReservationPaymentDto.class,false));
            reservationDto.setReservationAccountDto(ClassConverUtil.copyPropertiesToDto(reservationAccount, ReservationAccountDto.class,false));
            return reservationDto;
        }else {
            throw new JetfireException(JetfireSysExceptionEnum.RESER_IS_NOT_EXIST);
        }
    }

    @Override
    public void updateReservationAccountRefundSta(ReservationPayInsideParamDto reservationPayInsideParamDto)throws Exception{
        ReservationAccount queryrb=new ReservationAccount();
        queryrb.setLhotelGroupCode(reservationPayInsideParamDto.getLhotelGroupCode());
        queryrb.setL2RNo(reservationPayInsideParamDto.getOrderNo());
        ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryrb);
        reservationAccount.setRefundSta(reservationPayInsideParamDto.getRefundSta());
        commonService.merge(reservationAccount);
    }

    private void requestRefund(ReservationBase reservationBase)throws Exception{
        String l2rNo = reservationBase.getL2RNo();
        ReservationAccount queryrb=new ReservationAccount();
        queryrb.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
        queryrb.setL2RNo(l2rNo);
        ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryrb);
        if(reservationAccount!=null&&ReservationConstant.L2RPAYSTA.DONE.equals(reservationAccount.getL2rPaysta())) {
            if (BaseRateplanConstant.CANCELTYPE.CUSTOM.equals(reservationBase.getCancelType())) {
                L2RCancelRuleDto cancelRule = JacksonUtil.jsonToBean(reservationBase.getCancelRule(), L2RCancelRuleDto.class);
                Date canCancelDate = DateUtil.addHours(DateUtil.parseDateNoTime(reservationBase.getArr()), 24 - cancelRule.getCancelHour());
                if (canCancelDate.getTime() < reservationBase.getRealCancelDate().getTime()) {
                    throw new JetfireException(JetfireSysExceptionEnum.ABOVE_CANCEL_DATE, "实际发起取消时间为：" + reservationBase.getRealCancelDate());
                } else {
                    BigDecimal mutiply = new BigDecimal(1);
                    for (L2RCancelRuleBaseDto ruleBase : cancelRule.getL2RCancelRuleBaseDtos()) {
                        Date dateNode = DateUtil.addHours(DateUtil.parseDateNoTime(reservationBase.getArr()), 24 - ruleBase.getHourNode());
                        if (reservationBase.getRealCancelDate().getTime() <= dateNode.getTime()) {
                            if (mutiply.compareTo(ruleBase.getMultiplyValue()) > 0) {
                                mutiply = ruleBase.getMultiplyValue();
                            }
                        }
                    }
                    ReservationPrice queryPrice = new ReservationPrice();
                    queryPrice.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
                    queryPrice.setLhotelCode(reservationBase.getLhotelCode());
                    queryPrice.setL2RNo(l2rNo);
                    QueryResult<ReservationPrice> reservationPriceQueryResult = commonService.findAllEQ(ReservationPrice.class, queryPrice);
                    if (reservationPriceQueryResult.getResultlist() != null && reservationPriceQueryResult.getResultlist().size() > 0) {
                        BigDecimal refund = new BigDecimal(0);
                        if (new BigDecimal(1).compareTo(mutiply) != 0) {
                            if (new BigDecimal(0).compareTo(mutiply) == 0) {
                                refund =reservationAccount.getDeposit().subtract(reservationAccount.getL2RPromotion());
                            } else {
                                //总支付金额-集团（联房优惠金额）-需扣除房费=需退款金额
                                refund = reservationAccount.getDeposit().subtract(reservationAccount.getL2RPromotion()).subtract(reservationPriceQueryResult.getResultlist().get(0).getAmountSale().multiply(mutiply)).setScale(2);
                            }
                        }
                        if(new BigDecimal(0).compareTo(refund)<0){
                            this.refund(reservationAccount, reservationBase.getL2RNo(), refund, reservationBase.getCancelRemark());
                        }
                    } else {
                        throw new JetfireException(JetfireSysExceptionEnum.EVERYDAY_PRICE_NOT_EXIT);
                    }
                }
            } else {
                BigDecimal refund = reservationAccount.getResRatePaySale();
                this.refund(reservationAccount, reservationBase.getL2RNo(), refund, reservationBase.getCancelRemark());
            }
        }
    }

    private void refund(ReservationAccount reservationAccount,String l2rNo,BigDecimal refund,String cancelRemark) throws Exception {
        if(ReservationConstant.L2RPAYSTA.DONE.equals(reservationAccount.getL2rPaysta())){
            reservationAccount.setRefundSta(ReservationConstant.RefundSta.REFUNDING);
            reservationAccount.setRefund(refund);
            commonService.merge(reservationAccount);
            RefundManageApplyRefundParamDto paramDto=new RefundManageApplyRefundParamDto();
            paramDto.setOrderNo(l2rNo);
            paramDto.setRefundFee(refund);
            paramDto.setRemark(cancelRemark);
            refundManageFeignService.applyRefund(paramDto);
        }else {
            throw new JetfireException(JetfireSysExceptionEnum.L2R_PAYSTA_NOT_DONE);
        }
    }
}
